﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using IPR_StudyMeetingMrgSys.ViewModel;
using IPR_StudyMeetingMrgSys.Models;
using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.BLL;
using IPR_StudyMeetingMrgSys.Model;
using Newtonsoft.Json;
using log4net;
using System.Reflection;
using System.Data;
namespace IPR_StudyMeetingMrgSys.Controllers
{
    public class AttendanceSetController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);
        [HttpGet]
        [Route("api/AttendanceSet/GetAttendanceSet")]
        public IHttpActionResult GetAttendanceSet()
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                DataSet dsSearch = new IPR_StudyMeetingMrgSys.BLL.AttendanceSet().GetList(" AutoID=1 ");
                bool blExist = false;
                if (dsSearch != null)
                {
                    if (dsSearch.Tables.Count > 0)
                        if (dsSearch.Tables[0].Rows.Count > 0)
                        {
                            //返回token信息
                            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                            resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                            resultMsg.Data = dsSearch.Tables[0];
                            resultMsg.Count = dsSearch.Tables[0].Rows.Count;
                            blExist = true;
                        }
                }
                if (!blExist)
                {
                    //返回token信息
                    resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                    resultMsg.Info = "查询结果不存在";
                    resultMsg.Data = null;
                }
            }
            catch (Exception exc)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = "内部请求出错 " + exc.Message;
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 修改设备信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/AttendanceSet/Edit")]
        public IHttpActionResult UpdateAttendanceSet(Model.AttendanceSet model)
        {
            ResultMsg resultMsg = new ResultMsg();
            if (new IPR_StudyMeetingMrgSys.BLL.AttendanceSet().Update(model))
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "更新失败";
                resultMsg.Data = "";
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 添加设备信息
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/AttendanceSet/Add")]
        public IHttpActionResult AddAttendanceSet(Model.AttendanceSet model)
        {
            ResultMsg resultMsg = new ResultMsg();
            string ErrorMsg = string.Empty;
           var rows=new IPR_StudyMeetingMrgSys.BLL.AttendanceSet().Add(model);
            if (rows> 0)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                resultMsg.Data = "添加成功";
            }
            else
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = "添加失败";
                resultMsg.Data = ErrorMsg;
            }
            return Json(resultMsg);
        }
        
    }
}
